//
//  DJMyOpenCreatorVC.m
//  daojiVideo
//
//  Created by tikeios_002 on 2024/6/7.
//

#import "DJMyOpenCreatorVC.h"
#import "AgreementViewController.h"
#import "PQIAPManager.h"
#import "DJMyFeedBackVC.h"

@interface DJMyOpenCreatorVC ()
@property (weak, nonatomic) IBOutlet UIView *xiyiView;
@property (weak, nonatomic) IBOutlet UIButton *tongyiButton;
@property (weak, nonatomic) IBOutlet UILabel *xieyiTip;
@property (weak, nonatomic) IBOutlet UIButton *seeButton;
@property (weak, nonatomic) IBOutlet UILabel *now_label;
@property (weak, nonatomic) IBOutlet UILabel *old_label;
@property (weak, nonatomic) IBOutlet UIView *openButton;
@property (weak, nonatomic) IBOutlet UIView *tip_view;
@property (weak, nonatomic) IBOutlet UILabel *tip_label;


@property (strong, nonatomic) NSDictionary * vipdata;
@property (weak, nonatomic) IBOutlet UIView *quanyi_dian1;
@property (weak, nonatomic) IBOutlet UIView *quanyi_dian2;
@property (weak, nonatomic) IBOutlet UIView *showBgView;
@property (weak, nonatomic) IBOutlet UIView *showView1;
@property (weak, nonatomic) IBOutlet UIView *showView2;
@property (weak, nonatomic) IBOutlet UIView *showView3;
@property (weak, nonatomic) IBOutlet UIView *showView4;
@property (weak, nonatomic) IBOutlet UIView *showView5;
@property (weak, nonatomic) IBOutlet UIView *showView6;
@property (weak, nonatomic) IBOutlet UIView *showView7;
@end

@implementation DJMyOpenCreatorVC

-(void)showTextViewNavViewTo1{
    NSMutableArray * showTextViewNavViewNameArrTo1 = [NSMutableArray new];
    UIView * showTextViewNavViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewNavViewViewTo1.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewNavViewViewTo1];
    UILabel * showTextViewNavViewLabelTo1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    NSInteger showTextViewNavViewNumberTo1 = arc4random_uniform(20);
    NSLog(@"：%ld", (long)showTextViewNavViewNumberTo1);
    showTextViewNavViewLabelTo1.text = [NSString stringWithFormat:@"%ld",(long)showTextViewNavViewNumberTo1];
    [showTextViewNavViewLabelTo1 addSubview:showTextViewNavViewLabelTo1];
    
    UIImageView * showTextViewNavViewLabelTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [showTextViewNavViewViewTo1 addSubview:showTextViewNavViewLabelTo2];
    
}
- (void)showOldAddNavViewToMainOldView1{
    UIView * oldBgSS = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    oldBgSS.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:oldBgSS];
    
    UIButton * sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-75, kStatusBarHeight+10, 60, 46)];
    sendBtn.layer.cornerRadius = 3;
    sendBtn.backgroundColor = [UIColor whiteColor];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:ColorHex(@"#333333") forState:UIControlStateNormal];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendBtn addTarget:self action:@selector(sendTap) forControlEvents:UIControlEventTouchUpInside];
    [oldBgSS addSubview:sendBtn];
    
    UIView * sendBg = [[UIView alloc]initWithFrame:CGRectMake(15, kStatusBarHeight+66, KScreen_W-30, (KScreen_H-KSafeAreaBottomHeight-25-(kStatusBarHeight+66))/2)];
    sendBg.backgroundColor = [UIColor whiteColor];
    sendBg.layer.cornerRadius = 3;
    [oldBgSS addSubview:sendBg];
    
    UILabel * sendTip = [[UILabel alloc]initWithFrame:CGRectMake(0, (sendBg.frame.size.height-40)/2, sendBg.frame.size.width, 40)];
    sendTip.text = @"发送区";
    sendTip.textColor = ColorHex(@"#f0f0f0");
    sendTip.textAlignment = NSTextAlignmentCenter;
    sendTip.font = [UIFont systemFontOfSize:28];
    [sendBg addSubview:sendTip];
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationItem.title = @"开通视频创作";
    self.view.backgroundColor = viewback_black;
    self.showBgView.backgroundColor = subviewback_black;

    self. showBgView.layer.cornerRadius = 15;
    self.showView1.layer.cornerRadius = 8;
    self.showView2.layer.cornerRadius = 8;
    self.showView3.layer.cornerRadius = 8;
    self.showView4.layer.cornerRadius = 8;
    self.showView5.layer.cornerRadius = 8;
    self.showView6.layer.cornerRadius = 8;
    self.showView7.layer.cornerRadius = 8;
    
    self.showView1.backgroundColor = viewback_black;
    self.showView2.backgroundColor = viewback_black;
    self.showView3.backgroundColor = viewback_black;
    self.showView4.backgroundColor = viewback_black;
    self.showView5.backgroundColor = viewback_black;
    self.showView6.backgroundColor = viewback_black;
    self.showView7.backgroundColor = viewback_black;
    
    self.quanyi_dian1.layer.cornerRadius = 4;
    self.quanyi_dian2.layer.cornerRadius = 4;
    self.xieyiTip.text = NSLocalizedString(@"我已阅读并同意", nil);
    [self.seeButton setTitle:NSLocalizedString(@"《视频创作开通协议》", nil) forState:UIControlStateNormal];
    self.view.backgroundColor = viewback_black;
    self.openButton.layer.cornerRadius = 10;
    self.openButton.backgroundColor = MainColor;
    self.tip_view.layer.cornerRadius = 5;
    self.tip_label.textColor = MainColor;
    [self.tip_view setTransform:CGAffineTransformMakeRotation(-0.1)];
    
    [self getVipList];
}
- (IBAction)navBackTap:(id)sender {
    if(self.isPresent){
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)getVipList{
    [SVProgressHUDTool showWithStatus:nil];
    KMutableDic(sendDic);
    sendDic[@"type"] = @(3);
    KWeakSelf
    [YBNetBase memberAccountVipListRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            [SVProgressHUDTool dismiss];
            weakSelf.vipdata = data[@"list"][0];
            weakSelf.now_label.text = [NSString stringWithFormat:@"%d元",[weakSelf.vipdata[@"price"] intValue]];
            weakSelf.old_label.text = [NSString stringWithFormat:@"￥%d元",[weakSelf.vipdata[@"cost_price"] intValue]];
            weakSelf.tip_label.text = [NSString stringWithFormat:@"限时%0.1f折",[weakSelf.vipdata[@"price"] floatValue]/[weakSelf.vipdata[@"cost_price"] floatValue]*10];
//            weakSelf.now_label.text = [NSString stringWithFormat:@"629元"];
//            weakSelf.old_label.text = [NSString stringWithFormat:@"￥1258元"];
//            weakSelf.tip_label.text = [NSString stringWithFormat:@"限时0.5折"];
            weakSelf.openButton.hidden = NO;
            weakSelf.tip_view.hidden = NO;
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}
-(void)showTextViewAddShowViewTo2{
    UIView * showTextViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 20, 100, 360)];
    showTextViewViewTo1.backgroundColor = [UIColor whiteColor];
    showTextViewViewTo1.layer.cornerRadius = 8;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewViewTo1];
    
    UIImageView * showTextViewimgTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(K_AUTOLAYOUT_W(157.5), K_AUTOLAYOUT_W(260), K_AUTOLAYOUT_W(60), K_AUTOLAYOUT_W(60))];
    showTextViewimgTo2.layer.cornerRadius = K_AUTOLAYOUT_W(30);
    showTextViewimgTo2.clipsToBounds = YES;
    showTextViewimgTo2.contentMode = UIViewContentModeScaleAspectFill;
    showTextViewimgTo2.hidden = YES;
    [showTextViewViewTo1 addSubview:showTextViewimgTo2];
    
    UILabel *  showTextViewimgTo3 = [[UILabel alloc]initWithFrame:CGRectMake(K_AUTOLAYOUT_W(15), K_AUTOLAYOUT_W(35), K_AUTOLAYOUT_W(345), K_AUTOLAYOUT_W(25))];
    showTextViewimgTo3.font = FontBold(18);
    showTextViewimgTo3.textColor = ThreeColor;
    showTextViewimgTo3.textAlignment = NSTextAlignmentCenter;
    showTextViewimgTo3.hidden = YES;
    [showTextViewViewTo1 addSubview:showTextViewimgTo3];
}
- (NSString *)showTextViewGetNowTimerTo3{
    NSDateFormatter *showTextViewGetNowFa = [[NSDateFormatter alloc] init];
    [showTextViewGetNowFa setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [showTextViewGetNowFa stringFromDate:[NSDate date]];
}
- (IBAction)agreenClick:(id)sender {
    self.tongyiButton.selected = !self.tongyiButton.selected;
}
- (IBAction)seeClick:(id)sender {
    AgreementViewController * web = [[AgreementViewController alloc]init];
    web.modalPresentationStyle = UIModalPresentationFullScreen;
    web.url = [QZUserInfoModel share].cre;
    web.Nav_title = NSLocalizedString(@"视频创作服务协议", nil);
    web.isPerSent = YES;
    [self presentViewController:web animated:YES completion:nil];
}
- (IBAction)openTap:(id)sender {
    if(self.tongyiButton.selected == NO){
        [YBMsgPop showPop:NSLocalizedString(@"请阅读并同意协议", nil)];
        return;
    }
    KWeakSelf;
    KMutableDic(sendDic)
    sendDic[@"vipid"] = self.vipdata[@"vipid"];
    //获取订单
    [SVProgressHUDTool showWithStatus:nil];
    [YBNetBase memberAccountOrderCreateRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            [weakSelf startAppleBuyID:data[@"order_no"]];
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}
- (void)startAppleBuyID:(id)orderId{
    [SVProgressHUDTool showWithStatus:nil];
    KWeakSelf
    NSString *identifier = self.vipdata[@"productid"];
    dispatch_async(dispatch_get_main_queue(), ^{
        // 需要在主线程执行的代码
        [[PQIAPManager sharedManager] requestProductWithId:identifier orderId:orderId type:0 buySuccess:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                // 需要在主线程执行的代码
                [SVProgressHUDTool dismiss];
                [QZUserInfoModel share].iscreator = @(1);
                [SVProgressHUDTool showSuccessWithStatus:NSLocalizedString(@"恭喜你,视频创作服务开通成功", nil) completion:^{
                    [weakSelf navBackTap:nil];
                }];
            });
        } buyFailuer:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                // 需要在主线程执行的代码
                [SVProgressHUDTool dismiss];
            });
        } checkFailuer:^(NSString *str) {
            dispatch_async(dispatch_get_main_queue(), ^{
                // 需要在主线程执行的代码
                [SVProgressHUDTool dismiss];
                [[BSystemAlert shared] showAlertWithTitle:NSLocalizedString(@"温馨提示", nil) message:str userData:nil cancelTitle:NSLocalizedString(@"取消", nil) actions:@[NSLocalizedString(@"前往反馈", nil)] actionBlock:^(id  _Nonnull userData, NSString * _Nonnull actionTitle) {
                    if ([actionTitle isEqualToString:NSLocalizedString(@"前往反馈", @"前往设置")]) {
                        DJMyFeedBackVC *suggVC = [[DJMyFeedBackVC alloc] init];
                        [weakSelf.navigationController pushViewController:suggVC animated:YES];
                    }
                }];

            });
        }];
    });
}

- (void)showTextViewSubViewToViewTo2:(UIView * )subView{

    UIView * showTextViewSubViewTo2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewSubViewTo2.clipsToBounds = YES;
    [subView addSubview:showTextViewSubViewTo2];
    UILabel * showTextViewSubViewLabelTo3 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    showTextViewSubViewLabelTo3.text = @"ppppp88888";
    [showTextViewSubViewTo2 addSubview:showTextViewSubViewLabelTo3];
    
    UIImageView * showTextViewSubViewTo3 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [subView addSubview:showTextViewSubViewTo3];

}
@end
